<?php

/**
 * This is the model class for table "{{page_master}}".
 *
 * The followings are the available columns in table '{{page_master}}':
 * @property integer $page_id
 * @property string $page_name
 * @property string $template
 * @property integer $created_by
 * @property string $created_date
 * @property string $updated_date
 * @property integer $status
 */
class PageMaster extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return PageMaster the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{page_master}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('page_name, status, template, slug', 'required'),
			array('created_by, status', 'numerical', 'integerOnly'=>true),
			array('page_name, template,url,title,meta_keyword,meta_description', 'length', 'max'=>200),
			array('updated_date', 'default', 'value' => new CDbExpression('NOW()'), 'setOnEmpty' => false, 'on' => 'update'), 
			array('created_date,updated_date', 'default', 'value' => new CDbExpression('NOW()'), 'setOnEmpty' => false, 'on' => 'insert'),
			array('created_by', 'default', 'value' => ApplicationSessions::run()->read('id'), 'setOnEmpty' => false),
			
			array('page_id, page_name, template, created_by, created_date, updated_date, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'page_id' => 'Page',
			'page_name' => 'Page Name',
			'template' => 'Template',
			'created_by' => 'Created By',
			'created_date' => 'Created Date',
			'updated_date' => 'Updated Date',
			'status' => 'Status',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('page_id',$this->page_id);
		$criteria->compare('page_name',$this->page_name,true);
		$criteria->compare('template',$this->template,true);
		$criteria->compare('created_by',$this->created_by);
		$criteria->compare('created_date',$this->created_date,true);
		$criteria->compare('updated_date',$this->updated_date,true);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function getModel($type, $arrParams = array())
    {
        switch ($type)
        {
            case 'BANNER-ID':
                return BannerDetails::model()->findByPk($arrParams['banner_id']);
                break; 
                
			case 'PAGE-ID':
                return PageMaster::model()->findByPk($arrParams['page_id']);
                break;
                
			case 'LOC-ID':
                return PageLocationMapping::model()->findByPk($arrParams['mapping_id']);
                break;
                
            case 'LOC-ID-NAME':
                $a = PageLocationMapping::model()->findByPk($arrParams['mapping_id']);
                if(!empty($a))
				return $a->getAttribute('location_name');
				else
				return '';
                break;	
                    
            case 'PAGE-ID-NAME':
                $a = PageMaster::model()->findByPk($arrParams['page_id']);   
                if(!empty($a))
				return $a->getAttribute('page_name');
				else
				return '';
                break;	
			
        }
    }
	public static function getList($type, $arrParams = array())
    {
        switch ($type)
        {
            case 'ALL':
                return PageMaster::model()->findAll('status=1');
                break;

        }
    }
    
	
}